#!/usr/bin/perl
#lvcreate.cgi
require './lvm-lib.pl';
&ReadParse();
my ($err_msg, $found, $lvname, @pvinfo, @vginfo, @lvinfo, $l, $lv, $v, $vg);
$lvname = $in{'lv'};
if(!$lvname){
	$err_msg = $text{'err_noselectlv'};
	goto ERROR_OUT;
}
my $fstype = get_fstype($lvname);
if($fstype eq "xfs"){
	$err_msg = "XFS filesystem resize not supported by now.";
	goto ERROR_OUT;
}
@vginfo = get_vg_info();
@lvinfo = get_lv_info();
#@pvinfo = get_pv_info();
$found = 0;
foreach $l (@lvinfo) {
	if($l->{'lv'} eq $lvname){
		$lv = $l;
		$found = 1;
	}
}
if(!$found){
	$err_msg = $text{'err_lvnofound'};
	goto ERROR_OUT;
}
$vgfound=0;
foreach $v (@vginfo) {
	if($v->{'vg'} eq $lv->{'vg'}){
		$vg = $v;
		$vgfound = 1;
		last;
	}
}
if(!$vgfound){
	$err_msg = $text{'err_vgnofound'};
	goto ERROR_OUT;
}
my $maxsize = $lv->{'size'} + $vg->{'free'};
&header($text{'title_lvconfig'}, "", "configlv", 0, 1);
print "<form name=form_lvconfig action=index.cgi method=post>\n";
print "<table class=mode1 width=80%>";
print "<tr><td width=200><b>$text{'index_lv'}</b></td><td><b>$lv->{'lvname'}</b></td></tr>\n";
print "<tr><td><b>$text{'index_vg'}</b></td><td>$lv->{'vg'}</td></tr>\n";
print "<tr><td><b>$text{'index_vg_free'}</b></td><td>$vg->{'free'} MB</td></tr>\n";
print "<tr><td width=200><b>$text{'index_lv_size'}</b></td><td>$lv->{'size'} MB</td></tr>\n";
print "<tr><td width=200><b>$text{'newsize'}</b></td>";
print "<td>";
print "<input type=text name=newsize class=text> ";
print "<input type=button class=sbutton name=maxsize value=$text{'maxsize'} onclick='return setmaxsize($maxsize);' title='Use all free size.'> MB";
print "</td></tr>\n";

#print "<tr><td width=200><b>Logical Volume Size</b></td>";
#print "<td><input name=lvsize class=text size=10> GB</td></tr>\n";
print "</table>\n";

print "<table border=0 class=mode1>\n";
print "<tr><td>";
print "<input type=submit name=config class=button1 value=$text{'bt_submit'} onclick='return SetEditValue(this)'>\n"; 
print "</td>";
print "<td><input type=submit name=cancel class=button1 value=$text{'bt_cancel'}></td>";
print "</tr>\n";
print "</table>\n";
print "<input type=hidden name=lvname value=$lv->{'lv'}>\n";
print "<input type=hidden name=lvsize value=$lv->{'size'}>\n";
print "<input type=hidden name=maxsize value=$maxsize>\n";
print "<input type=hidden name=vgfree value=$vg->{'free'}>\n";
print "<input type=hidden name=lvpath value=$lv->{'lvpath'}>\n";
print <<EOF;
<script>
function setmaxsize(ms)
{
	document.forms[0].newsize.value = ms;
}

function SetEditValue(obj)
{
        if (obj == document.form_lvconfig.config) {
                document.form_lvconfig.action = 'act_lvconfig.cgi';
        }
}

function SetReduceVg(m)
{
		if (m == 1){
			document.form_vgconfig.config.disabled=true;
		}
		else
		{
			document.form_vgconfig.config.disabled=false;
		}

		document.form_vgconfig.freepv.disabled = true;
		document.form_vgconfig.vgpv.disabled = false;
}
function SetExtendVg(n)
{
		if (n == 0){
			document.form_vgconfig.config.disabled=true;
		}
		else
		{
			document.form_vgconfig.config.disabled=false;
		} 
		document.form_vgconfig.freepv.disabled = false;
		document.form_vgconfig.vgpv.disabled = true;
}
</script>
EOF
print "</form>\n";
&footer("/lvm", $text{'index'});
return;
ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);
